package com.ruoyi.web.controller.task;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.system.domain.Order;
import com.ruoyi.system.service.GoodsService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.service.OrderService;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Component
@ConditionalOnProperty(value = "spring.profiles.active", havingValue = "prod")
public class OrderTask {

    @Resource
    OrderService orderService;
    @Resource
    GoodsService goodsService;

    public static void main(String[] args) {
        System.out.println(UUID.fastUUID().toString().replace("-", ""));
    }


    //变成超时状态
    @Scheduled(cron = "0 0/1 * * * ? ")

    public void timeout() {
        Order order = new Order();
        order.setStatus("paid");
        List<Order> list = orderService.findPage(order, new PageDomain()).getList();
        if (CollectionUtil.isEmpty(list)) {
            return;
        }
        for (int i = 0; i < list.size(); i++) {
            Order order1 = list.get(i);
            DateTime dateTime = DateUtil.offsetMinute(order1.getCreateTime(), 5);
            if (dateTime.getTime() < new Date().getTime()) {
                orderService.timeout(order1.getId());
            }
        }
    }

    //有交易记录的图片 释放金额
//    @Scheduled(cron = "0 0/2 * * * ? ")
    @Scheduled(cron = "0/5 * * * * ? ")
    public void release() {

        Order order = new Order();
        order.setFreezeStatus(1);
        List<Order> list = orderService.findPage(order, new PageDomain()).getList();
        if (CollectionUtil.isEmpty(list)) {
            return;
        }
        for (int i = 0; i < list.size(); i++) {
            Order order1 = list.get(i);
            if (StrUtil.isBlank(order1.getTradePic())) {
                continue;
            }
            //上传图后  5分钟
            DateTime dateTime = DateUtil.offsetMinute(order1.getTradeTime(), 5);
            if (dateTime.getTime() < new Date().getTime()) {
                orderService.release(order1.getId());
            }
        }
    }

    @Resource
    private ISysUserService userService;

    //超过30分钟的  自动关闭抢单功能
    @Scheduled(cron = "0 0/1 * * * ? ")
    public void updateStatus() {
        SysUser sysUser = new SysUser();
        sysUser.setOrderStatus(1);
        PageDomain pageDomain = new PageDomain();
        pageDomain.setOrderByColumn("orderStatusTime");
        pageDomain.setPageSize(50);
        List<SysUser> list = userService.findPage(sysUser, pageDomain).getList();
        if (CollectionUtil.isEmpty(list)) {
            return;
        }
        for (int i = 0; i < list.size(); i++) {
            sysUser = list.get(i);
            if (sysUser.getOrderStatusTime() == null) {
                continue;
            }
            DateTime dateTime = DateUtil.offsetMinute(sysUser.getOrderStatusTime(), Constants.closeTime);
            if (dateTime.getTime() < new Date().getTime()) {
                sysUser.setOrderStatus(0);
                userService.updateDomain(sysUser);
            }
        }

    }



}
